package com.aaa.ssm.controller;

import com.aaa.ssm.pojo.dto.EmpDTO;
import com.aaa.ssm.pojo.dto.PermissionDTO;
import com.aaa.ssm.result.AjaxResult;
import com.aaa.ssm.service.EmpService;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("emp")
public class EmpController {
    @Autowired
    private EmpService empService;

    @Autowired
    private HttpSession session;
    @RequestMapping("login")
    public AjaxResult login(String username,String password){
        try {
            EmpDTO login = empService.login(username, password);
            session.setAttribute("emp",login);
            return AjaxResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
    @RequestMapping("getInfo")
    public AjaxResult getInfo(){
        try {
            //获取员工信息
            EmpDTO emp = (EmpDTO) session.getAttribute("emp");
            //根据员工信息查询当前员工的权限信息
            List<PermissionDTO> permission = empService.getPermission(emp.getRid());

            HashMap<Object,Object> map=new HashMap<>();
            map.put("userInfo",emp);
            map.put("permissionInfo",permission);
            return AjaxResult.success(map);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

}
